package com.zxl.live.screen.screen.ui.widget.panel.heart;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;

import java.util.ArrayList;
import java.util.List;

public class HeartSpirit {

	private float angle = 10;
	
	public Garden garden;
	
	private List<PointF> heart;
	
	public boolean isFinish;
	
	public HeartSpirit() {
		garden = new Garden();
		heart = new ArrayList<PointF>();
		isFinish = false;
	}
	
	public PointF getHeartPoint(float angle) {
		float t = (float) (angle / Math.PI);
		float x = (float) (19.5f * (16f * Math.pow(Math.sin(t), 3)));
		float y = (float) (- 20f * (13f * Math.cos(t) - 5f * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t)));
		return new PointF(x, y);
	}
	
	public void draw(Canvas canvas, Paint paint, int alpha) { 
		garden.render(canvas, paint, alpha);
	}
	
	public void reset() {
		angle = 10;
		heart.clear();
		isFinish = false;
		garden.clear();
	}

	public void logic(int offsetX, int offsetY) {
		PointF bloom = getHeartPoint(angle);
		bloom.offset(offsetX, offsetY);
		boolean draw = true;
		for (int i = 0; i < heart.size(); i++) {
			PointF p = heart.get(i);
			float distance = (float) Math.sqrt(Math.pow(p.x - bloom.x, 2) + Math.pow(p.y - bloom.y, 2));
			if (distance < Garden.BLOOMRADIUS_MAX * 1.3) {
				draw = false;
				break;
			}
		}
		if (draw) {
			heart.add(bloom);
			garden.createRandomBloom(bloom.x, bloom.y);
		}		
		if (angle >= 30) {
			isFinish = true;
		} else {
			angle += 0.2f;
			isFinish = false;
		}
	}
	
}


